package hotel.compare.oversea.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.controller.BaseController;
import hotel.base.oversea.utils.Result;
import hotel.compare.oversea.entity.master.CompareHotel;
import hotel.compare.oversea.mapper.master.CompareHotelMapper;
import hotel.compare.oversea.service.CompareHotelService;
import hotel.compare.oversea.vo.CompareHotelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 校验后酒店
 *
 * @author Zhang·JZ
 * @date 2020-05-28
 */
@Slf4j
@RestController
@RequestMapping("/compareHotel")
@Api(value = "CompareHotelController", tags = {"校验后酒店管理接口"})
public class CompareHotelController extends BaseController {

    @Autowired
    private CompareHotelService compareHotelService;

    @Autowired
    private CompareHotelMapper compareHotelMapper;

    @PostMapping("/queryOneNotCreateMasterHotelId")
    @ApiOperation(value = "返回一条未创建子酒店的母酒店id", notes = "必须经过auth授权")
    public Result<Object> queryOneNotCreateMasterHotelId(@RequestBody Map map) {
        log.info("返回一条未创建子酒店的母酒店id  进入方法 queryOneNotCreateMasterHotelId  jsonObject={} ", JSON.toJSONString(map));
        Integer index = (Integer) map.get("index");
        Integer pageSize = (Integer) map.get("pageSize");
        return compareHotelService.queryOneNotCreateMasterHotelId(index, pageSize);
    }

    @PostMapping("/findNotCreateMtHotelId")
    @ApiOperation(value = "返回有所未创建过子酒店的美团id", notes = "必须经过auth授权")
    public Result<Object> findNotCreateMtHotelId() {
        return compareHotelService.findNotCreateMtHotelId();
    }

    @GetMapping("/updateCompanreMasterIsCreateSub")
    @ApiOperation(value = "更新对比母酒店的创建子酒店状态", notes = "必须经过auth授权")
    public Result<Object> updateCompanreMasterIsCreateSub(@RequestParam String masterHotelId, @RequestParam Integer isCreateSub, @RequestParam String isCreateSubRemark) {
        return compareHotelService.updateCompareMasterIsCreateSub(masterHotelId, isCreateSub, isCreateSubRemark);
    }


    /**
     * test token
     *
     * @return
     */
    @GetMapping("/testToken")
    public Result testToken() {
        String retStr = "-------> token 调用成功";
        log.info(retStr);
        return new Result(retStr);
    }


    /**
     * 开始校验酒店信息
     *
     * @return
     */
    @GetMapping("/compareHotel")
    @ApiOperation(value = "校验酒店信息")
    public Result<Boolean> compareHotel() {
        return compareHotelService.compareHotel();
    }

    /**
     * 获取校验后酒店信息
     *
     * @return
     */
    @GetMapping("/getCompareHotel")
    @ApiOperation(value = "获取校验后酒店信息")
    public Result<List<CompareHotel>> getCompareHotel() {

        List<CompareHotel> compareHotelList = compareHotelService.getCompareHotel();
        return new Result<>(compareHotelList);
    }


    /**
     * 根据携程master_hotel_id 获取校验后酒店信息
     *
     * @return
     */
    @GetMapping("/getCompareHotelByMasterHotelId")
    @ApiOperation(value = "根据携程master_hotel_id 获取校验后酒店信息")
    public Result getCompareHotelByMasterHotelId(@RequestParam String masterHotelId) {

        return compareHotelService.getCompareHotelByMasterHotelId(masterHotelId);
    }

    /**
     * 根据携程master_hotel_id获取对应的美团酒店id
     */
    @PostMapping("/getCompareMtHotelId")
    @ApiOperation(value = "根据携程master_hotel_id 获取对应的美团酒店id")
    public Result<Integer> getCompareMtHotelId(@RequestBody JSONObject jsonObject) {
        int masterHotelId = jsonObject.getInteger("masterHotelId");
        String mtHotelId = compareHotelService.getCompareMtHotelId(masterHotelId);
        return new Result<>(Integer.valueOf(mtHotelId));
    }
    /***
    * @Author: lmf
    * @Date: 2020/12/11
     * 根据美团酒店id获取携程母酒店id
    */
    @PostMapping("/getCompareHotelId")
    @ApiOperation(value = "根据携程master_hotel_id 获取对应的美团酒店id")
    public Result<Integer> getCompareHotelId(@RequestBody JSONObject jsonObject) {
        int mtHotelId = jsonObject.getInteger("mtHotelId");
        Integer masterHotelId = compareHotelService.getCompareHotelId(mtHotelId);
        return new Result<>(masterHotelId);
    }



    /**
     * <H1>获取核验后酒店信息列表</H1>
     *
     * @param compareHotelVO
     * @return hotel.base.oversea.utils.Result
     * @author HuangT
     * @date 2020/7/24 16:37
     */
    @PostMapping("/getCompareHotelList")
    public Result getCompareHotelList(@RequestBody(required = false) CompareHotelVO compareHotelVO) {
        return compareHotelService.getCompareHotelList(compareHotelVO);
    }


    /**
     * <H1>更新状态</H1>
     *
     * @param compareHotel
     * @return hotel.base.oversea.utils.Result
     * @author HuangT
     * @date 2020/7/30 16:59
     */
    @PutMapping("changeIsCompare")
    public Result changeIsCompare(@RequestBody(required = false) CompareHotelVO compareHotel) {
        return compareHotelService.changeIsCompare(compareHotel);
    }

    /**
     * <H1>获取对比信息</H1>
     *
     * @param id
     * @return hotel.base.oversea.utils.Result
     * @author HuangT
     * @date 2020/7/31 10:23
     */
    @GetMapping("getHotelInfoById")
    public Result getHotelInfoById(String id) {
        return compareHotelService.getHotelInfoById(id);
    }

    /**
     * <H1>手动新增对比酒店</H1>
     *
     * @param compareHotel
     * @return hotel.base.oversea.utils.Result
     * @author HuangT
     * @date 2020/8/10 17:17
     */
    @PostMapping("addCompare")
    public Result addCompare(@RequestBody(required = false) CompareHotelVO compareHotel) {
        return compareHotelService.addCompare(compareHotel);
    }


    /**
     * <H1>新增前验证是否存在已验证通过的酒店</H1>
     *
     * @param compareHotel
     * @return hotel.base.oversea.utils.Result
     * @author HuangT
     * @date 2020/8/10 18:21
     */
    @PostMapping("checkExist")
    public Result checkExist(@RequestBody(required = false) CompareHotelVO compareHotel) {
        return compareHotelService.checkExist(compareHotel);
    }


    /**
     * <H1>通过对比信息</H1>
     */
    @GetMapping("compareHotelByPhone")
    public void compareHotelByPhone() {
        log.info("----> 进入compareHotelByPhone");
        compareHotelService.compareHotelByPhone();
    }


    /**
     * <H1>获取美团ID</H1>
     *
     * @param compareHotelForFpVO 实体对象
     * @return hotel.base.oversea.utils.Result<java.util.List < java.lang.String>>
     * @author HuangT
     * @Date 2020/9/21 20:56
     */
//    @PostMapping("getMtHotelIds")
//    public Result<List<CompareRoomForFpVO>> getMtHotelIds(@RequestBody CompareHotelForFpVO compareHotelForFpVO) {
//        return compareHotelService.getMtHotelIds(compareHotelForFpVO);
//    }

    /**
     *
     * @author: lmf
     * @time: 2020/11/16
     * 导入待创建的酒店，修改效验表的酒店状态
     */
    @PostMapping("/submitUploadUp")
    @ApiOperation(value = "导入酒店数据,开启酒店售卖", notes = "必须经过auth授权")
    public Result<Object> submitUploadSale(@RequestParam("excelFile") MultipartFile excelFile) {
        if(excelFile.isEmpty()){
            return new Result<>("上传失败");
        }
        return  compareHotelService.submitUploadUp(excelFile);
    }

    @GetMapping("/updatePhone")
    public void updatePhone(){
        new Thread(new Runnable() {

            //修改携程电话
            @Override
            public void run() {
                updateSwPhone();
            }
        }).start();


        new Thread(new Runnable() {

            //修改美团电话
            @Override
            public void run() {
                updateMtPhone();
            }
        }).start();
    }


    public void updateSwPhone() {
        long startTime = System.currentTimeMillis();
//        List<CompareHotel> compareHotelList = new ArrayList<>();
//        compareHotelList = compareHotelMapper.selectAll();
        List<CompareHotel> swPhoneList = new ArrayList<>();
        List<CompareHotel> upList = new ArrayList<>();
        swPhoneList = compareHotelMapper.getSwPhoneList();


        int count = 5 ;

        if(!swPhoneList.isEmpty() && swPhoneList.size()>0){

//            int dataSize = swPhoneList.size();
//            int remainder = dataSize % count;  //(先计算出余数)
//            int number = dataSize / count;  //然后是商
//            int offset = 0;//偏移量
            for (int i=0;i<swPhoneList.size();i++){
//                List<CompareHotel> cutList = new ArrayList<>();
//                if (remainder > 0) {
//                    cutList = swPhoneList.subList(i * number + offset, (i + 1) * number + offset + 1);
//                    remainder--;
//                    offset++;
//                } else {
//                    cutList = swPhoneList.subList(i * number + offset, (i + 1) * number + offset);
//                }
//                int upSwPhone = compareHotelMapper.updateSwPhoneBySwHotelId(cutList);

                int upSwPhone = compareHotelMapper.updateSwPhoneBySwHotelId(swPhoneList.get(i));

                log.info(" upSwPhone:{} ",upSwPhone);

                long endTime = System.currentTimeMillis();

                log.info(" 执行时间time:{} ",(endTime-startTime));
            }
        }



        long endTime = System.currentTimeMillis();

        log.info(" 执行总时间time:{} ",(endTime-startTime));

    }


    public void updateMtPhone() {
        long startTime = System.currentTimeMillis();
        List<CompareHotel> mtPhoneList = new ArrayList<>();
        List<CompareHotel> upList = new ArrayList<>();
        mtPhoneList = compareHotelMapper.getMtPhoneList();


        int count = 5 ;

        if(!mtPhoneList.isEmpty() && mtPhoneList.size()>0){

//            int dataSize = swPhoneList.size();
//            int remainder = dataSize % count;  //(先计算出余数)
//            int number = dataSize / count;  //然后是商
//            int offset = 0;//偏移量
            for (int i=0;i<mtPhoneList.size();i++){
//                List<CompareHotel> cutList = new ArrayList<>();
//                if (remainder > 0) {
//                    cutList = swPhoneList.subList(i * number + offset, (i + 1) * number + offset + 1);
//                    remainder--;
//                    offset++;
//                } else {
//                    cutList = swPhoneList.subList(i * number + offset, (i + 1) * number + offset);
//                }
//                int upSwPhone = compareHotelMapper.updateSwPhoneBySwHotelId(cutList);

                int upSwPhone = compareHotelMapper.updateMtPhoneByMtHotelId(mtPhoneList.get(i));

                log.info(" upSwPhone:{} ",upSwPhone);

                long endTime = System.currentTimeMillis();

                log.info(" 执行时间time:{} ",(endTime-startTime));
            }
        }



        long endTime = System.currentTimeMillis();

        log.info(" 执行总时间time:{} ",(endTime-startTime));

    }
}
